<?php
function lilina_admin_create($args)
{
    if (!xarVarFetch('url','str:1:',$url)) return;
    if (!xarVarFetch('keyword','str:1:',$keyword)) return;
    extract($args);
    if (!xarSecConfirmAuthKey()) return;
    if (!ereg("^http://|https://|ftp://", $url)) {
        $msg = xarML('Invalid Address for Feed');
        xarErrorSet(XAR_USER_EXCEPTION, 'MISSING_DATA', new DefaultUserException($msg));
        return;
    }

    // Check for Duplicate Feed
        // Create the cache
    $links = xarModAPIFunc('lilina',
                           'user',
                           'getall');
    foreach ($links as $link){
        if ($link['url'] == $url){
            $msg = xarML('Duplicate Feed URL');
            xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
            return;
        }
    }

    // Not a Duplicate, so grab it and process it.

    $feeds = xarModAPIFunc('lilina',
                          'user',
                          'process',
                          array('feedfile' => $url));

    if (!isset($feeds['warning'])){
        //$linkid = $link['id'];
        $chantitle = $feeds['chantitle'];
        $chanlink = $feeds['chanlink'];
        $content = $feeds['feedcontent'];
        $ico = '';

        foreach ($content as $feed => $feedcontent){
        //$test = var_export($feedcontent, true); echo "<pre>$test</pre>"; return;
            $item_id = md5($feedcontent['link']);  
            if (!isset($feedcontent['date'])){
                $feedcontent['date'] = time();
            }
            if ($feedcontent['date'] > time()){
                $feedcontent['date'] = time();
            }
            $data['items']['chantitle'] = $chantitle;
            if(!xarModAPIFunc('lilina',
                              'user',
                              'update_story',
                              array('title'     => $feedcontent['title'],
                                    'summary'   => $feedcontent['description'],
                                    'link'      => $feedcontent['link'],
                                    'timestamp' => $feedcontent['date'],
                                    'ctitle'    => $chantitle,
                                    'curl'      => $chanlink,
                                    'cfavicon'  => $ico,
                                    //'linkid'    => $linkid,
                                    'unique'    => $item_id,
                                    'keyword'   => $keyword))) return;
            
        }
    }

    // So everything works out, add the feed to be scheduled for normal delivery
    $id = xarModAPIFunc('lilina',
                        'admin',
                        'create',
                         array('url' => $url,
                               'keyword' => $keyword));

    if ($id == false) return;   
    
    xarResponseRedirect(xarModURL('lilina', 'admin', 'main'));
    return true;
}
?>